package me.chan.streamTrade;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.joining;

public class App {
	
	public static void main(String[] args) {
		Trader raoul = new Trader("Raoul", "Cambridge");
		Trader mario = new Trader("Mario", "Milan");
		Trader alan = new Trader("Alan", "Cambridge");
		Trader brian = new Trader("Brian", "Cambridge");
		
		List<Transaction> transactions = Arrays.asList(
				new Transaction(brian, 2011, 300),
				new Transaction(raoul, 2012, 1000),
				new Transaction(raoul, 2011, 400),
				new Transaction(mario, 2012, 700),
				new Transaction(mario, 2012, 710),
				new Transaction(alan, 2012, 950)
		);
		
/*		List<Transaction> _2011Transaction = transactions.stream().filter(d -> d.getYear() == 2011)
								.sorted(comparing(Transaction::getValue))
													.collect(toList());
		System.out.println(_2011Transaction);
*/		
	/*	List<String> traderInDifferenceCities = transactions.stream().map(d->d.getTrader().getCity())
																				.distinct().collect(toList());
		System.out.println(traderInDifferenceCities);
		
		*/
		/*
	 List<Trader> traderFromCam = transactions.stream().map(Transaction::getTrader)
	 
																											.filter(t -> "Cambridge".equals(t.getCity()))
																											.distinct()
																											.sorted(comparing(Trader::getName))
																											.collect(toList());
		System.out.println(traderFromCam);
	*/	
		List<String> traderName1 = transactions.stream().map(t->t.getTrader().getName()).distinct()
															.sorted().collect(toList());
		/*String traderName2 = transactions.stream().map(t->t.getTrader().getName()).distinct()
				.sorted().reduce("", (str1, str2) -> str1 + str2 + '\t');
		
		System.out.println(traderName2);
*/
		System.out.println(traderName1);
		//more effective !
		String traderName = transactions.stream().map(t->t.getTrader().getName()).distinct()
												.sorted().collect(joining());
		System.out.println(traderName);
	/*	Optional<Trader> traderInMilan = transactions.stream().map(Transaction::getTrader).distinct()
																		.filter(t-> "Milan".equals(t.getCity())).findAny();
		System.out.println(traderInMilan.get());
		*/
		
	/*	
		Integer sumCamTraderValue  = transactions.stream().filter(t -> "Cambridge".equals(t.getTrader().getCity()))
															.map(t -> t.getValue()).reduce(0, (a,b) -> a+b).intValue();
		System.out.println(sumCamTraderValue);
		
		transactions.stream().filter(t-> "Cambridge".equals(t.getTrader().getCity())).map(Transaction::getValue)
													.forEach(System.out::println);
	 */
		
	/*	Optional<Integer> maxTraderValue = transactions.stream().map(t-> t.getValue()).reduce(Integer::max);
		System.out.println(maxTraderValue.get());
		*/
		
		Optional<Transaction> minValueTrans = transactions.stream().min(comparing(Transaction::getValue));
		System.out.println(minValueTrans.get());
	}

}